package com.telecomyt.jwportal.activity.collect;

import com.telecomyt.jwportal.activity.collect.bean.CollectBean;
import com.telecomyt.jwportal.base.BasePresenter;
import com.telecomyt.jwportal.http.RetrofitManager;
import com.telecomyt.jwportal.utils.ObjectSaveUtils;
import com.telecomyt.jwportal.utils.ToastUtils;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @date: 2020/9/8
 * @author: chengDa
 * @description:
 */
public class CollectPresenter extends BasePresenter<CollectContract.Model, CollectContract.View> implements CollectContract.Presenter {

    public CollectPresenter(LifecycleProvider<ActivityEvent> provider) {
        super(provider);
    }

    @Override
    public void getCollectData(int userId, int tenantId, int requestCode) {
        Observable<CollectBean> localDataObservable = Observable.create((ObservableOnSubscribe<CollectBean>)
                sub -> {
                    CollectBean cacheData = null;
                    try {
                        cacheData = (CollectBean) ObjectSaveUtils.getObject(getContext(), "collect_cache");   //读取数据对象
                    } catch (Exception e) {

                    }
                    if (cacheData != null) {
                        cacheData.setCache(true);
                        sub.onNext(cacheData);
                    }
                    sub.onComplete();
                })
                .subscribeOn(Schedulers.io());
        Observable<CollectBean> remoteDataObservable = RetrofitManager.getInstance()
                .getDefaultReq()
                .favorite(userId, tenantId)
                .subscribeOn(Schedulers.io())
                .map(dataResponse -> {
                    CollectBean data = dataResponse.getData();
                    if (data != null && data.getRecords() != null && data.getRecords().size() > 0) {
                        data.setCache(false);
                        ObjectSaveUtils.saveObject(getContext(), "collect_cache", data);   //保存数据对象
                    }
                    return data;
                });

        Observable.concat(localDataObservable, remoteDataObservable)
                .firstElement()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CollectBean>() {
                    @Override
                    public void accept(CollectBean collectBean) throws Exception {
                        boolean isCache = collectBean.isCache();
                        if (isCache) {
                            ToastUtils.showShort("缓存数据");
                        } else {
                            ToastUtils.showShort("网络数据");
                        }
                        getView().setCollectData(collectBean, requestCode);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                });
//            getView().showLoading();
//            RetrofitManager.getInstance()
//                    .getDefaultReq()
//                    .favorite(userId, tenantId)
//                    .compose(RxHelper.observableIOMain((Activity) getProvider()))
//                    .subscribe(new BaseObserver<CollectBean>() {
//                        @Override
//                        public void onSuccess(CollectBean bean) {
//                            getView().dismissLoading();
//                            if (bean != null) {
//                                ObjectSaveUtils.saveObject(getContext(), "collect_cache", bean);   //保存数据对象
//                                getView().setCollectData(bean, requestCode);
//                            }
//                        }
//
//                        @Override
//                        public void onFailure(Throwable e, String errorMsg) {
//                            getView().dismissLoading();
//                            ToastUtils.showShort(errorMsg);
//                        }
//                    });
    }


    @Override
    protected CollectContract.Model creatModule() {
        return null;
    }


}
